<?php
class SHML_Content
{

	public function content_section($properties)
	{
		$p = explode("/",$_SERVER['PHP_SELF']);
		$page = array_pop($p);
		$p = implode("/",$p) . "/";
		return $p;
	}
	
	public function content_selected($properties)
	{
		$s = $properties['attributes']['selected'];
		$s = (substr($s, 0, 1) != " ") ? " " . $s : $s;
		$kill = (strtolower($properties['attributes']['kill']) == "true") ? TRUE : FALSE;
		$n = $properties['innerHTML'];
		$http = (empty($_SERVER['HTTPS'])) ? "http" : "https";
		$uri = $http . "//" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		
		preg_match_all('/<a.*?href=["|\'](.*?)["|\'].*?>(.*?)<\/a>/i', $n, $match);
		foreach ($match[0] as $k => $v) {
			$href = trim($match[1][$k]);
			$href = (empty($href) || substr($href, 0, 1) == "#") ? $_SERVER['REQUEST_URI'] : $href;
			$text = trim($match[2][$k]);
			$n = preg_replace("/(#SELECTED#)/i", "#" . ($k + 1) . "#", $n, 1);
			if (stristr($_SERVER['REQUEST_URI'], $href)) {
				if ($kill === TRUE) {
					$n = preg_replace('/(<a.*?href=(["|\'])' . preg_quote($href, "/") . '\\2.*?>(.*?)<\/a>)/i', $text, $n);
				}
				
				if (!empty($s)) {
					$n = preg_replace("/(#" . ($k + 1) . "#)/i", $s, $n);
				}
			}
			else {
				$n = preg_replace("/(#" . ($k + 1) . "#)/i", "", $n);
			}
		}

		return $n;
	}
	
	public function content_url($properties)
	{
		$v = strtolower($properties['attributes']['value']);
		$s = (strtolower($properties['attributes']['secure']) == "true") ? "s" : "";
		$p = "";
		
		switch ($v) {
			case "full":
			$p = $_SERVER['REQUEST_URI'];
			break;
			
			case "section":
			$p = $this->content_section(1);
			break;
			
			case "current":
			$p = $_SERVER['PHP_SELF'];
			break;
		}
		
		return "http" . $s . "://" . $_SERVER['HTTP_HOST'] . $p;
	}
	
	
}